/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package vue;

import ia_multiagent1.Agent;
import ia_multiagent1.Espace2D;
import ia_multiagent1.LogAgent;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

/**
 *
 * @author Lady Koala
 */
public class JPanelGrid extends JPanel {
      	
    private int hauteurFenetre; 
    private int largeurFenetre;
 
    private int dimGridCases ;
    private int dimPixelCase ;
     
    private Image imageAgent;
    private Image[] imageRessource;
    private Agent myAgent;
    private Vector<Agent> mesAgents;
    
    
    
    private Espace2D espace2D;
    
    private boolean onlyValeurChamps = false;
    
    private boolean afficherHistoire = false; // Indique quel champ consulter, i.e. si plusieurs agents seront affichés ou non
    private boolean presencePheromones = false ;
    private  boolean disparitionPheromone = false;
    private  boolean grilleInitialisee = false;
    private boolean coneDeflection = false;
    private boolean afficherCone = false;

    public void setGrilleInitialisee(boolean grilleInitialisee) {
        this.grilleInitialisee = grilleInitialisee;
    }
    
    public void setPresencePheromones(boolean presencePheromones) {
        this.presencePheromones = presencePheromones;
    }  
      
    public void setDisparitionPheromone(boolean disparitionPheromone) {
        this.disparitionPheromone = disparitionPheromone;
    }
    
    public void setSuiviAgent(boolean afficherHistoire) {
        this.afficherHistoire = afficherHistoire;
    }
    
    public void setConeDeflection(boolean cone){
        this.coneDeflection = cone;
    }

    public void setAfficherCone(boolean cone){
        this.afficherCone = cone;
    }

    public void setOnlyValeurChamps(boolean onlyValeurChamps) {
        this.onlyValeurChamps = onlyValeurChamps;
    }
    
    
    public JPanelGrid(int dimGridCases, int dimPixelCase, Agent agent, Espace2D espace2D,boolean onlyValeurChamps, boolean presencePheromones){
        this.dimPixelCase = dimPixelCase;       
        this.hauteurFenetre = (this.dimPixelCase+1)*dimGridCases+1;
        this.largeurFenetre = (this.dimPixelCase+1)*dimGridCases+1;
        this.setSize(hauteurFenetre, largeurFenetre);
        this.dimGridCases = dimGridCases;
        this.afficherHistoire = true;
        this.myAgent = agent;
        this.espace2D = espace2D;
        this.onlyValeurChamps = onlyValeurChamps;
        this.presencePheromones = presencePheromones;
        
        
        try {
            this.imageAgent = ImageIO.read(new File("src/images/alienmoche10.jpg"));
            this.imageRessource = new Image[3];
            this.imageRessource[0] = ImageIO.read(new File("src/images/ressource1.jpg"));
            this.imageRessource[1] = ImageIO.read(new File("src/images/ressource2.jpg"));
            this.imageRessource[2] = ImageIO.read(new File("src/images/ressource3.jpg"));
        } catch (IOException ex) {
            Logger.getLogger(JPanelGrid.class.getName()).log(Level.SEVERE, null, ex);
        }  
    }
    

    
    public JPanelGrid(int dimGridCases, int dimPixelCase, Vector<Agent> agents, Espace2D espace2D,boolean onlyValeurChamps, boolean presencePheromones){
        this.dimPixelCase = dimPixelCase;       
        this.hauteurFenetre = (this.dimPixelCase+1)*dimGridCases+1;
        this.largeurFenetre = (this.dimPixelCase+1)*dimGridCases+1;
        //this.setSize(hauteurFenetre, largeurFenetre);
        this.dimGridCases = dimGridCases;
        this.afficherHistoire = false;
        this.mesAgents = agents;
        this.espace2D = espace2D;
        
        this.onlyValeurChamps = onlyValeurChamps;
        this.presencePheromones = presencePheromones;
        try {
            //this.imageAgent = ImageIO.read(new File("src/images/alienmoche10.jpg"));
            this.imageAgent = ImageIO.read(new File("src/images/agent13.bmp"));
            this.imageRessource = new Image[3];
//            this.imageRessource[0] = ImageIO.read(new File("src/images/ressource1.jpg"));
//            this.imageRessource[1] = ImageIO.read(new File("src/images/ressource2.jpg"));
//            this.imageRessource[2] = ImageIO.read(new File("src/images/ressource3.jpg"));
            this.imageRessource[0] = ImageIO.read(new File("src/images/or.bmp"));
            this.imageRessource[1] = ImageIO.read(new File("src/images/bois.bmp"));
            this.imageRessource[2] = ImageIO.read(new File("src/images/petrole.bmp"));
            
        } catch (IOException ex) {
            Logger.getLogger(JPanelGrid.class.getName()).log(Level.SEVERE, null, ex);
        }  
    }
    
    public void paintGrid(Graphics2D g2d, int tailleGrille){
       
        if (!grilleInitialisee){
            Color cFont = new Color(255,255,230);
        
            g2d.setColor(cFont);
                
            g2d.fillRect(0, 0, tailleGrille, tailleGrille);
        
            grilleInitialisee =true;
        }
        
        g2d.setColor(Color.BLACK);
        
        //on trace les premières
        g2d.drawLine(0, 0, 0, hauteurFenetre);
        g2d.drawLine(0, 0, largeurFenetre, 0);
        
        for(int i=0; i<=dimGridCases; i++){
            //on trace les ièmes
            g2d.drawLine((dimPixelCase+1)*i+1, 0, (dimPixelCase+1)*i+1, hauteurFenetre);
            g2d.drawLine(0, (dimPixelCase+1)*i+1, largeurFenetre, (dimPixelCase+1)*i+1);
        }
        
        for (int i=0; i<dimGridCases; i++){
            for(int j=0; j<dimGridCases; j++){
                dessinerCaseXY(g2d,i,j);
            }
        }
    }

    
    public void dessinerCaseXY(Graphics2D g2d, int i, int j){   
        
        this.eraseCase(g2d, i, j);
        if (onlyValeurChamps){
//            Integer textint = (Integer) espace2D.getCase(i, j).getAttraction();  
            String textint = espace2D.mesPointsAttractionToString();
            String text = textint.toString();
            this.drawString(text, g2d, i, j);
        }
        
        if(!afficherHistoire){
        //  presencePheromones = true;
        if (presencePheromones){
            try {
                int niveauGrisSurCase = espace2D.getCase(i, j).getQtePheromones();
                
//                if (niveauGrisSurCase != 0){
//                 System.out.println("sur la case "+ i + "," + j +" nous avons "+niveauGrisSurCase +" pheromones");
//                }
                this.drawCarreGris(5*niveauGrisSurCase, g2d, i, j);                
            } catch (Exception ex) {
                Logger.getLogger(JPanelGrid.class.getName()).log(Level.SEVERE, null, ex);
            }
        }       
        try {
            if (this.espace2D.getCase(i, j).estVideDeRessources()){
                //ne redessine pas la ressource dessus
            }
            else{
                drawRessource(g2d, this.espace2D.getCase(i, j).getRessource(), i, j);
            }
        } catch (Exception ex) {
            Logger.getLogger(JPanelGrid.class.getName()).log(Level.SEVERE, null, ex);
        } 
        }
    }
    
    
    
    public void eraseCase(Graphics2D g2d, int i, int j){
        int x = i*(dimPixelCase+1) + 2;
        int y = j*(dimPixelCase+1) + 2;
              
        Color cFont = new Color(255,255,230);  
        //Color cFont = new Color(255,0,0); 
        g2d.setColor(cFont);        
        g2d.fillRect(x, y, dimPixelCase, dimPixelCase);
        //g2d.setColor(Color.BLACK);
    }
       
    
    @Override
    public void paint(Graphics g){
        
        int tailleGrille = (this.dimGridCases*(dimPixelCase+1)+1);
        
        Graphics2D g2d = (Graphics2D)g;
        
        paintGrid(g2d, tailleGrille);
       
        
        /* Dessin des pheromones */   
        /* TTTTTTTTTTTTTTTTTTTTTTTT */
        /* myAgent ====> premierAgent */
        /* BBBBBBBBBBBBBBBBBBBBBBBBBbb*/ 
        
        // SUPPRIMER LA METHODE POUR UN SEUL AGENT ?????
        //  myAgent = mesAgents.elementAt(0);

        if (!afficherHistoire){
        for (int i=0; i<mesAgents.size(); i++){
            Agent agent = mesAgents.elementAt(i);
            /*if (agent.getHistoireAgent().size() > 1){
                //eraseCase(g2d, agent.getXPrecedent(), agent.getYPrecedent());
                //  dessinerCaseXY(g2d, agent.getXPrecedent(), agent.getYPrecedent());*/
            if (afficherCone) {
                drawHistoire(g2d, agent.getHistorique());
            }
            /*}*/
            drawAgent(g2d, agent.getX(), agent.getY());
        }
        }
        else{
            drawAgent(g2d, myAgent.getX(), myAgent.getY());
            drawHistoire(g2d, myAgent.getHistorique());
        }
    }
    
    
    
    public void drawAgent(Graphics2D g2d, int i, int j){
        this.drawImage(imageAgent, g2d, i, j);
    }
    
    
    
    private void drawImage(Image img, Graphics2D g2d, int i, int j){
        int x = i*(dimPixelCase+1) + 2;
        int y = j*(dimPixelCase+1) + 2;
        g2d.drawImage(img,x,y,this);
    }
    
    
    private void drawCarreGris(int niveauGris, Graphics2D g2d, int i, int j) throws Exception{
        int x = i*(dimPixelCase+1) + 2;
        int y = j*(dimPixelCase+1) + 2;
        
        if (niveauGris<0){
            throw new Exception("problem.le niveau de gris doit etre compris entre 0 et 255. helas, il vaut : "+ niveauGris);   
        }
        if (niveauGris>240){
            niveauGris = 240;
        }
        if (niveauGris == 0){
            Color c = new Color(255,255,230);
            g2d.setColor(c);
            g2d.fillRect(x, y, dimPixelCase, dimPixelCase);
        }
        else{
            Color c = new Color(255-niveauGris,255-niveauGris,255-niveauGris);
            g2d.setColor(c);
            g2d.fillRect(x, y, dimPixelCase, dimPixelCase);
        }
    }
        
        
 
    
   private void drawString(String text, Graphics2D g2d, int i, int j){
       int x = i*(dimPixelCase+1) + 2;
       int y = j*(dimPixelCase+1) + 2 + dimPixelCase;
        
       Font font = new Font("Courier", Font.ITALIC, 12);
       g2d.setFont(font);

       g2d.drawString(text, x, y);
    }
    
    
    public void drawDeplacementLine(Graphics2D g2d, int i1, int j1, int i2, int j2){
        
        int x1 = i1*(dimPixelCase+1) + 2 + dimPixelCase/2;
        int y1 = j1*(dimPixelCase+1) + 2 + dimPixelCase/2;
        int x2 = i2*(dimPixelCase+1) + 2 + dimPixelCase/2;
        int y2 = j2*(dimPixelCase+1) + 2 + dimPixelCase/2;
        g2d.setColor(Color.GREEN);
        g2d.drawLine(x1, y1, x2, y2);
        
    }
    
    public void drawHistoire(Graphics2D g2d, Vector<LogAgent> histoire){
        for(int i=0; i<histoire.size()-1; i++){
            this.drawDeplacementLine(g2d, histoire.elementAt(i).getX(),
                                          histoire.elementAt(i).getY(),
                                          histoire.elementAt(i+1).getX(),
                                          histoire.elementAt(i+1).getY());
        }
    }
    
    public void drawRessource(Graphics2D g2d, int type, int i, int j) throws Exception{
        g2d.setColor(new Color(255,255,204));
        switch(type){
            case 0 :
                // Pas de ressource, ne rien faire
                break;
            case 1:
                this.drawImage(imageRessource[0], g2d, i, j);
                break;
            case 2:
                this.drawImage(imageRessource[1], g2d, i, j);
                break;
            case 3:
                this.drawImage(imageRessource[2], g2d, i, j);
                break;
            default :
                System.err.println("Numéro de ressource inconnu.");
                throw new Exception("Ressource inconnue...");
        }
    }
    

}
